import { defineStore } from 'pinia'
import { ref } from 'vue'
import { issueApi } from '@/api/issue'
import type { Issue } from '@/types'

export const useIssueStore = defineStore('issue', () => {
  // 状态
  const issues = ref<Issue[]>([])
  const currentIssue = ref<Issue | null>(null)
  const loading = ref(false)

  // 获取所有问题
  const fetchIssues = async () => {
    loading.value = true
    try {
      const response = await issueApi.getAll()
      issues.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据项目ID获取问题
  const fetchIssuesByProject = async (projectId: number) => {
    loading.value = true
    try {
      const response = await issueApi.getByProject(projectId)
      issues.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取分配给我的问题
  const fetchMyAssignedIssues = async () => {
    loading.value = true
    try {
      const response = await issueApi.getMyAssigned()
      issues.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取我报告的问题
  const fetchMyReportedIssues = async () => {
    loading.value = true
    try {
      const response = await issueApi.getMyReported()
      issues.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据状态获取问题
  const fetchIssuesByStatus = async (status: string) => {
    loading.value = true
    try {
      const response = await issueApi.getByStatus(status)
      issues.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据优先级获取问题
  const fetchIssuesByPriority = async (priority: string) => {
    loading.value = true
    try {
      const response = await issueApi.getByPriority(priority)
      issues.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取问题详情
  const fetchIssue = async (id: number) => {
    loading.value = true
    try {
      const response = await issueApi.getById(id)
      currentIssue.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 创建问题
  const createIssue = async (issueData: Omit<Issue, 'id' | 'createdAt' | 'updatedAt'>) => {
    loading.value = true
    try {
      const response = await issueApi.create(issueData)
      issues.value.unshift(response)
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新问题
  const updateIssue = async (id: number, issueData: Partial<Issue>) => {
    loading.value = true
    try {
      const response = await issueApi.update(id, issueData)
      const index = issues.value.findIndex(i => i.id === id)
      if (index !== -1) {
        issues.value[index] = response
      }
      if (currentIssue.value?.id === id) {
        currentIssue.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 删除问题
  const deleteIssue = async (id: number) => {
    loading.value = true
    try {
      await issueApi.delete(id)
      issues.value = issues.value.filter(i => i.id !== id)
      if (currentIssue.value?.id === id) {
        currentIssue.value = null
      }
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新问题状态
  const updateIssueStatus = async (id: number, status: string) => {
    loading.value = true
    try {
      const response = await issueApi.updateStatus(id, status)
      const index = issues.value.findIndex(i => i.id === id)
      if (index !== -1) {
        issues.value[index] = response
      }
      if (currentIssue.value?.id === id) {
        currentIssue.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 分配问题
  const assignIssue = async (id: number, assigneeId: number) => {
    loading.value = true
    try {
      const response = await issueApi.assign(id, assigneeId)
      const index = issues.value.findIndex(i => i.id === id)
      if (index !== -1) {
        issues.value[index] = response
      }
      if (currentIssue.value?.id === id) {
        currentIssue.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 解决问题
  const resolveIssue = async (id: number) => {
    loading.value = true
    try {
      const response = await issueApi.resolve(id, 'Resolved')
      const index = issues.value.findIndex(i => i.id === id)
      if (index !== -1) {
        issues.value[index] = response
      }
      if (currentIssue.value?.id === id) {
        currentIssue.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 清空当前问题
  const clearCurrentIssue = () => {
    currentIssue.value = null
  }

  return {
    // 状态
    issues,
    currentIssue,
    loading,
    // 方法
    fetchIssues,
    fetchIssuesByProject,
    fetchMyAssignedIssues,
    fetchMyReportedIssues,
    fetchIssuesByStatus,
    fetchIssuesByPriority,
    fetchIssue,
    createIssue,
    updateIssue,
    deleteIssue,
    updateIssueStatus,
    assignIssue,
    resolveIssue,
    clearCurrentIssue
  }
})